from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest,time

import GreaterWMSyaml


def chrome_driver():
    driver = webdriver.Chrome()
    driver.implicitly_wait(10)
    driver.maximize_window()
    return driver
class GreaterWMSInstellTest(unittest.TestCase):
    cfg = GreaterWMSyaml.ReadYaml.read_yaml()
    def __init__(self, driver, url=cfg['url']):
        self.driver = driver
        self.url = url
        self.wait = WebDriverWait(driver=self.driver, timeout=5, poll_frequency=1.5)
        self.driver.get(url)
        time.sleep(15)

    def loc_wait(self, loc):
        return self.wait.until(EC.presence_of_element_located(loc))

    def loc_close(self):
        #按ESC取消业已打开的注册页面
        self.find_ele((By.XPATH,'/html/body/div[3]/div[2]/div/div[1]/button/span[2]/span/i')).click()

    def find_ele(self, loc):
        self.loc_wait(loc)
        return self.driver.find_element(*(loc))

    def find_eles(self, loc):
        self.loc_wait(loc)
        return self.driver.find_elements(*(loc))

    def set_language(self):
        self.find_ele((By.XPATH, '//i[text()="translate"]/ancestor::button')).click()
        self.find_ele((By.XPATH, '//div[text()="中文简体"]/parent::div')).click()
        time.sleep(5)

    def login(self, username='1', passwrod='1'):
        self.find_ele((By.XPATH, '//span[text()="登入"]/ancestor::button')).click()
        self.find_ele((By.XPATH, '//span[text()="管理员登入"]/ancestor::button')).click()
        self.find_ele((By.XPATH, '//input[@aria-label="管理员"]')).send_keys(username)
        self.find_ele((By.XPATH, '//input[@aria-label="密码"]')).send_keys(passwrod)
        self.find_ele((By.XPATH, '//div[contains(@class,"justify-start")]//span[text()="登入"]/ancestor::button')).click()
        time.sleep(1)

    def add_warehouse(self, whname=cfg['warehouse']['whname']):
        self.find_ele((By.XPATH, '//div[contains(text(),"仓库设置")]/parent::a')).click()
        self.driver.find_element(By.XPATH,'//*[@id="q-app"]/div/div[2]/main/div[2]/div/div[1]/'
                                          'div[1]/div[1]/button[1]/span[2]').click()
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]/label[1]'
                                          '/div/div[1]/div[1]/input').send_keys(whname)
    def add_city(self,cityname=cfg['warehouse']['cityname']):
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]/label[2]'
                                          '/div/div[1]/div/input').send_keys(cityname)
    def add_address(self,address=cfg['warehouse']['address']):
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]/label[3]'
                                          '/div/div[1]/div[1]/input').send_keys(address)
    def add_phone(self,phone=cfg['warehouse']['phone']):
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]/label[4]'
                                          '/div/div[1]/div/input').send_keys(phone)
    def add_director(self, director=cfg['warehouse']['director']):
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]/label[5]'
                                          '/div/div[1]/div/input').send_keys(director)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[3]/button[2]'
                                          '/span[2]/span').click()
        time.sleep(5)
    def location_settings(self):
        self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/main/div[1]'
                                 '/div/div/div/a[2]/div[2]/div').click()
        time.sleep(5)
    def add_settings(self,settingsname=cfg['warehouse']['settingsname']):
        self.driver.find_element(By.XPATH,'//span[@class="block" and text()="新增"]'
                                          '/ancestor::button').click()
        self.driver.find_element(By.XPATH,'html[1]/body[1]/div[3]/div[2]/div[1]'
                                          '/div[2]/label[1]/div[1]/div[1]/div[1]'
                                          '/input[1]').send_keys(settingsname)
        self.driver.find_element(*(By.XPATH, '//div[text()="库位尺寸"]/ancestor::label')).click()
        self.driver.find_element(*(By.XPATH, '//div[@class="q-item__label" '
                                             'and text()="间"]')).click()
        time.sleep(5)
        self.driver.find_element(*(By.XPATH, '//div[text()="库位属性"]/ancestor::label')).click()
        self.driver.find_element(*(By.XPATH, '//div[@class="q-item__label" '
                                         'and text()="Normal"]')).click()
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[3]'
                                          '/button[2]/span[2]/span').click()
        time.sleep(5)


    def location_size(self):
        self.find_ele((By.XPATH, '/html/body/div[1]/div/div[2]/main/div[1]'
                                 '/div/div/div/a[3]/div[2]/div')).click()
        time.sleep(5)

    def add_size(self,sizename=cfg['warehouse']['sizename'],
                 sizelen=cfg['warehouse']['sizelen'],
                 sizewidth=cfg['warehouse']['sizewidth'],
                 sizeheight=cfg['warehouse']['sizeheight']):

        self.driver.find_element(By.XPATH,'//span[@class="block" and text()="新增"]'
                                          '/ancestor::button').click()
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]'
                                          '/label[1]/div/div[1]/div/input').send_keys(sizename)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]'
                                          '/label[2]/div/div[1]/div/input').send_keys(sizelen)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]'
                                          '/label[3]/div/div[1]/div/input').send_keys(sizewidth)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[2]'
                                          '/label[4]/div/div[1]/div/input').send_keys(sizeheight)
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/div/div[3]'
                                          '/button[2]/span[2]/span').click()
        time.sleep(5)
class TestAddGoods(unittest.TestCase):

    def setUp(self):
        self.driver = chrome_driver()

    def test_01(self):
        page = GreaterWMSInstellTest(self.driver)
        page.loc_close()
        page.set_language()
        page.loc_close()
        page.login()
        page.add_warehouse()
        page.add_city()
        page.add_address()
        page.add_phone()
        page.add_director()
        page.location_size()
        page.add_size()
        page.location_settings()
        page.add_settings()

        time.sleep(5)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()